﻿using System;
using AzukiMap.Map;
using AzukiMap.MapSource;

namespace AzukiMap.Layer
{
    public class YahooMapLayer : MercatorQuadLayer
    {
        YahooMapSource _ymSource;

        public YahooMapLayer()
        {
            
        }

        public YahooMapLayer(YahooMapSource mapSource, string name)
            : base(mapSource, name)
        {
            _ymSource = mapSource;
        }

        protected override string CreateTileReqString(double tileScale, int tileLeft, int tileTop)
        {
            int level = GetScaleLevel(tileScale);
            int mapSize = GetMapSize(GetScaleLevel(tileScale));

            int tileX = ( tileLeft ) / _ymSource.TileWidth ;
            int tileY = ( mapSize / 2 - tileTop) / _ymSource.TileHeight - 1 ;

            YahooMapTileType tType = _ymSource.TileType;
            string version = _ymSource.TileVersions[tType];
            string additions = _ymSource.AdditionalParams[tType];

            //http://maps2.yimg.com/hx/tl?v=4.2&.intl=en&x=58156&y=7007&z=17&r=1
            //http://maps2.yimg.com/hx/tl?v=4.2&.intl=en&x=454&y=202&z=9&r=1
            return version + "&" + additions + "&x=" + tileX.ToString() 
                + "&y=" + tileY.ToString() + "&z=" + level.ToString() + "&r=1"; 
        }

        protected override int GetMapSize(int zoomLevel)
        {
            // YM zoom level number starts from 2.
            int size = (int)(_ymSource.TileWidth * 2 * Math.Pow(2.0, (double)(zoomLevel - 2)));
            return size;
        }

    }
}
